﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using KVB.Common;

namespace Customer_Profile.eCard.DBDao
{
    public class ProductDao
    {
        public static DataSet getAllProduct(string select_type)  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = {                                                 
                                                new SqlParameter("@select_type", SqlDbType.VarChar, 250) 
                                            };
                parameters[0].Value = select_type;
                ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "eCard_prod_getall",parameters);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.SearchCpartybyIDno", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }


        public static bool ProductAdd(
                        string procduct_code,
                        string procduct_name,
                        string sale_discount,
                        string commission_percent,
                        string settle_percent,
                        string annual_yeild,
                        string sale_costing,
                        string initial_amout,
                        string period,
                        string unit,
                        string unitRemark,
                        string in_use,
                        string if_appointDate,
                        string start_date,
                        string end_date,
                        string convert_ratio
            )  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            bool bok = false;
            try
            {                
                DB.Connection(ref conn);
                SqlParameter[] parameters = {                                                 
                                                new SqlParameter("@procduct_code", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@procduct_name", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@sale_discount", SqlDbType.Float) ,
                                                new SqlParameter("@commission_percent", SqlDbType.Float) ,
                                                new SqlParameter("@settle_percent", SqlDbType.Float) ,
                                                new SqlParameter("@annual_yeild", SqlDbType.Float) ,
                                                new SqlParameter("@sale_costing", SqlDbType.Float) ,
                                                new SqlParameter("@initial_amout", SqlDbType.Int) ,
                                                new SqlParameter("@period", SqlDbType.Int) ,
                                                new SqlParameter("@unit", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@unitRemark", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@in_use", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@if_appointDate", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@start_date", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@end_date", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@convert_ratio", SqlDbType.Float) 
                                            };                
                parameters[0].Value = procduct_code;
                parameters[1].Value = procduct_name;
                parameters[2].Value = float.Parse(sale_discount);
                parameters[3].Value = float.Parse(commission_percent);
                parameters[4].Value = float.Parse(settle_percent);

                parameters[5].Value = float.Parse(annual_yeild);
                parameters[6].Value = float.Parse(sale_costing);
                parameters[7].Value = int.Parse(initial_amout);
                parameters[8].Value = int.Parse(period);
                parameters[9].Value = unit;
                parameters[10].Value = unitRemark;
                parameters[11].Value = in_use;
                parameters[12].Value = if_appointDate;
                parameters[13].Value = start_date;
                parameters[14].Value = end_date;
                parameters[15].Value = float.Parse(convert_ratio);
                
                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_prod_Add", parameters);
                bok = true;
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("ProductDao.ProductAdd", e.Message);
                bok = false;
                throw e;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return bok;
        }

        public static bool ProductUpdate(
                       string procduct_code,
                       string procduct_name,
                       string sale_discount,
                       string commission_percent,
                       string settle_percent,
                       string annual_yeild,
                       string sale_costing,
                       string initial_amout,
                       string period,
                       string unit,
                       string unitRemark,
                       string in_use,
                       string if_appointDate,
                       string start_date,
                       string end_date,
                       string convert_ratio
           )  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            bool bok = false;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = {                                                 
                                                new SqlParameter("@procduct_code", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@procduct_name", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@sale_discount", SqlDbType.Float) ,
                                                new SqlParameter("@commission_percent", SqlDbType.Float) ,
                                                new SqlParameter("@settle_percent", SqlDbType.Float) ,
                                                new SqlParameter("@annual_yeild", SqlDbType.Float) ,
                                                new SqlParameter("@sale_costing", SqlDbType.Float) ,
                                                new SqlParameter("@initial_amout", SqlDbType.Int) ,
                                                new SqlParameter("@period", SqlDbType.Int) ,
                                                new SqlParameter("@unit", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@unitRemark", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@in_use", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@if_appointDate", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@start_date", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@end_date", SqlDbType.VarChar, 250) ,
                                                new SqlParameter("@convert_ratio", SqlDbType.Float) 
                                            };
                parameters[0].Value = procduct_code;
                parameters[1].Value = procduct_name;
                parameters[2].Value = float.Parse(sale_discount);
                parameters[3].Value = float.Parse(commission_percent);
                parameters[4].Value = float.Parse(settle_percent);
                parameters[5].Value = float.Parse(annual_yeild);
                parameters[6].Value = float.Parse(sale_costing);
                parameters[7].Value = int.Parse(initial_amout);
                parameters[8].Value = int.Parse(period);
                parameters[9].Value = unit;
                parameters[10].Value = unitRemark;
                parameters[11].Value = in_use;
                parameters[12].Value = if_appointDate;
                parameters[13].Value = start_date;
                parameters[14].Value = end_date;
                parameters[15].Value = float.Parse(convert_ratio);

                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_prod_Update", parameters);
                bok = true;
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("ProductDao.ProductUpdate", e.Message);
                bok = false;
                throw e;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return bok;
        }

        public static DataSet getAgentProduct(string agentno)  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = { new SqlParameter("@saleagent_id ", SqlDbType.VarChar, 50) };
                parameters[0].Value = agentno;

                ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "eCard_prod_byAgent",parameters);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.eCard_prod_byAgent", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static DataSet getProductbyProdcode(string prod_code)  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = { new SqlParameter("@prod_code ", SqlDbType.VarChar, 50) };
                parameters[0].Value = prod_code;

                ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "eCard_prod_bycode", parameters);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.getProductbyProdcode", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }
    }
}